package com.hjq.toast;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.view.WindowManager;

/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes2.dex */
public final class WindowHelper implements Application.ActivityLifecycleCallbacks {
    private final ToastHelper mToastHelper;
    private Activity mTopActivity;

    @Override // android.app.Application.ActivityLifecycleCallbacks
    public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
    }

    @Override // android.app.Application.ActivityLifecycleCallbacks
    public void onActivityStopped(Activity activity) {
    }

    private WindowHelper(ToastHelper toastHelper) {
        this.mToastHelper = toastHelper;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public static WindowHelper register(ToastHelper toastHelper, Application application) {
        WindowHelper windowHelper = new WindowHelper(toastHelper);
        application.registerActivityLifecycleCallbacks(windowHelper);
        return windowHelper;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public WindowManager getWindowManager() throws NullPointerException {
        Activity activity = this.mTopActivity;
        if (activity != null && !activity.isFinishing()) {
            return (WindowManager) this.mTopActivity.getSystemService("window");
        }
        throw null;
    }

    @Override // android.app.Application.ActivityLifecycleCallbacks
    public void onActivityCreated(Activity activity, Bundle bundle) {
        this.mTopActivity = activity;
    }

    @Override // android.app.Application.ActivityLifecycleCallbacks
    public void onActivityStarted(Activity activity) {
        this.mTopActivity = activity;
    }

    @Override // android.app.Application.ActivityLifecycleCallbacks
    public void onActivityResumed(Activity activity) {
        this.mTopActivity = activity;
    }

    @Override // android.app.Application.ActivityLifecycleCallbacks
    public void onActivityPaused(Activity activity) {
        if (this.mToastHelper.isShow()) {
            this.mToastHelper.cancel();
        }
    }

    @Override // android.app.Application.ActivityLifecycleCallbacks
    public void onActivityDestroyed(Activity activity) {
        if (this.mTopActivity == activity) {
            this.mTopActivity = null;
        }
    }
}
